home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / SPECTRAL.TST / GETGCD.CX < prev    next >
Text File  |  1996-03-09  |  2KB  |  78 lines

  1. /* ============ */
  2. /* getgcd.cx    */
  3. /* ============ */
  4. #include <xtendefs.h>
  5. #include <stdio.h>
  6. /* ==================================================================== */
  7. /* GetGCD - finds greatest common divisor by Euclid's algorithn        */
  8. /* ==================================================================== */
  9. void
  10. GetGCD(USHORT  *a, USHORT *b,  USHORT *m)
  11. {
  12.     USHORT  n[NE], t[NE];
  13.  
  14.     XCOPY(a, m); XABS(m); XFLOOR(m, m);
  15.     XCOPY(b, n); XABS(n); XFLOOR(n, n);
  16.  
  17.     if (XCMP(m, n) == -1)
  18.     {
  19.     XSWAP(m, n)
  20.     }
  21.     while (XTST(n) > 0)
  22.     {
  23.     XMOD(m, n, t);        /* t = mod(m,n) */
  24.     XCOPY(n, m);        /* m = n    */
  25.     XCOPY(t, n);        /* n = t    */
  26.     }
  27. }
  28. #define    TEST
  29. # undef TEST
  30. # ifdef TEST
  31. #define    MAIN    main
  32. #define    GETGCD    GetGCD
  33. #define    GETSTR    GetStr
  34. #define FLUSH_LINE(u)            \
  35.     {                    \
  36.     int    x;            \
  37.     do                \
  38.     {                \
  39.         x = getc(u);        \
  40.     }                \
  41.     while (x != EOF && x != '\n');    \
  42.     }
  43. /* ==================================================== */
  44. /* GetStr - prompts operator with s to get string Str    */
  45. /* ==================================================== */
  46. void
  47. GETSTR(char *s, char *Str)
  48. {
  49.     fprintf(stderr, "%s", s);
  50.  
  51.     scanf("%s", Str);
  52.  
  53.     FLUSH_LINE(stdin);
  54. }
  55. void
  56. MAIN()
  57. {
  58.     char    u[128], v[128];
  59.     USHORT  m[NE], n[NE], r[NE];
  60.  
  61.    puts("Testing Euclid's algorithm:  Needs two integers per call\n");
  62.  
  63.     while (main)
  64.     {
  65.     GETSTR("Enter first of the next pair: ", u);
  66.     GETSTR("Enter the second:             ", v);
  67.  
  68.     ASCTOX(u, m);
  69.     ASCTOX(v, n);
  70.     GETGCD(m, n, r);
  71. # if 0
  72.     printf("GCD = %.Lf\n", GETGCD(u,v));
  73. # endif
  74.     XPRINTF("GCD = ", r, 10);
  75.     }
  76. }
  77. # endif
  78.